var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all) __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if ((from && typeof from === 'object') || typeof from === 'function') {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, {
          get: () => from[key],
          enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
        });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (
  (target = mod != null ? __create(__getProtoOf(mod)) : {}),
  __copyProps(
    // If the importer is in node compatibility mode or this is not an ESM
    // file that has been converted to a CommonJS file using a Babel-
    // compatible transform (i.e. "__esModule" has not been set), then set
    // "default" to the CommonJS "module.exports" for node compatibility.
    isNodeMode || !mod || !mod.__esModule
      ? __defProp(target, 'default', { value: mod, enumerable: true })
      : target,
    mod,
  )
);
var __toCommonJS = (mod) => __copyProps(__defProp({}, '__esModule', { value: true }), mod);

// src/Cascader/AuthedCascader/index.tsx
var AuthedCascader_exports = {};
__export(AuthedCascader_exports, {
  default: () => AuthedCascader_default,
});
module.exports = __toCommonJS(AuthedCascader_exports);
var import_react = __toESM(require('react'));
var import_antd = require('antd');
var import_createField = __toESM(require('../../createField'));
var import_FieldContext = require('../../_utils/contexts/FieldContext');
var import_useRequestOptions = __toESM(require('../../_utils/hooks/useRequestOptions'));
var AuthedCascader = ({
  options: optionsProps,
  fieldNames,
  authedBy,
  value,
  onChange,
  ...props
}) => {
  const { label } = (0, import_FieldContext.useFieldContext)();
  const {
    value: valueKey = 'value',
    label: labelKey = 'label',
    children: childrenKey = 'children',
  } = fieldNames || {};
  const { options } = (0, import_useRequestOptions.default)(optionsProps);
  const innerOptions = (0, import_react.useMemo)(() => {
    if (authedBy === true)
      return {
        readonlyOptions: [],
        configOptions: options,
      };
    let readonlyOptions = [];
    let configOptions2 = [];
    const traverse = (opts, level = 0) => {
      const matched = opts.find((opt) => {
        return authedBy && opt[valueKey] === authedBy[level];
      });
      if (!matched) return;
      if (level + 1 === (authedBy == null ? void 0 : authedBy.length)) {
        configOptions2 = matched[childrenKey];
      }
      if (level < authedBy.length) {
        readonlyOptions.push(matched);
        if (matched.children) traverse(matched.children, level + 1);
      }
    };
    traverse(options);
    return {
      readonlyOptions,
      configOptions: configOptions2,
    };
  }, [options, authedBy]);
  const readonlyValues = (0, import_react.useMemo)(() => {
    const { readonlyOptions } = innerOptions;
    return readonlyOptions.map((item) => item[valueKey]);
  }, [innerOptions]);
  const readonlyLabels = (0, import_react.useMemo)(() => {
    const { readonlyOptions } = innerOptions;
    return readonlyOptions.map((item) => item[labelKey]);
  }, [innerOptions]);
  const configOptions = (0, import_react.useMemo)(() => {
    return innerOptions.configOptions;
  }, [innerOptions]);
  const handleChange = (0, import_react.useCallback)(
    (value2) => {
      if (onChange)
        if (value2) {
          onChange(!readonlyValues.length ? value2 : [...readonlyValues, ...value2]);
        } else {
          onChange(!readonlyValues.length ? value2 : [...readonlyValues]);
        }
    },
    [readonlyValues, onChange],
  );
  return /* @__PURE__ */ import_react.default.createElement(
    import_react.Fragment,
    null,
    readonlyLabels.length
      ? /* @__PURE__ */ import_react.default.createElement('span', null, readonlyLabels.join(''))
      : '',
    configOptions.length
      ? /* @__PURE__ */ import_react.default.createElement(import_antd.Cascader, {
          options: configOptions,
          placeholder: `请选择${label || ''}`,
          style: !readonlyLabels.length ? void 0 : { width: '72%', marginLeft: 5 },
          ...props,
          value:
            value !== void 0
              ? value.slice(!readonlyValues.length ? 0 : readonlyValues.length)
              : void 0,
          onChange: handleChange,
        })
      : null,
  );
};
var AuthedCascader_default = (0, import_createField.default)(AuthedCascader, {
  defaultDetailType: 'cascader',
});
